<html>
<head><meta charset="utf-8"><title>try match block · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/try.20match.20block.html">try match block</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="243834861"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/try%20match%20block/near/243834861" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Stefano Cefalu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/try.20match.20block.html#243834861">(Jun 24 2021 at 19:29)</a>:</h4>
<p>I was running into the problem of repeated Ok(...) wrappings in my match blocks recently. I could not just wrap the whole thing in Ok, as when none of the patterns matched and it fell to the default case, I wanted the block to result in Err(...). This repetition is something I've seen often with Option as well, and so I thought the language may benefit from a solution to this, for the sake of code readability. Seeing as how try is already a reserved keyword, I thought of the try match block, which would work very similarly to match. It would be something like below where x would attempt to be matched to the patterns, resulting in Ok(value) if the pattern is matched. If all patterns fail to match, then Err(TryMatchError) (or maybe Err(x)?) would be returned. This would be similar for Option, where it would return Some(value) on a successful match, and None when failing to match. Would this be a worthwhile addition to the language?</p>
<div class="codehilite"><pre><span></span><code>let temp: Result = try match x {
    /*Pattern*/ =&gt; /*value*/,
    /*Pattern*/ =&gt; /*value*/,
   ...
};
</code></pre></div>



<a name="243837847"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/try%20match%20block/near/243837847" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/try.20match.20block.html#243837847">(Jun 24 2021 at 19:53)</a>:</h4>
<p>What about something like this?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">temp</span>: <span class="nb">Result</span><span class="o">&lt;</span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kr">try</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nb">Ok</span><span class="p">(</span><span class="k">match</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">Pattern</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">value</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">Pattern</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">value</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="nb">Err</span><span class="p">(</span><span class="n">y</span><span class="p">)</span><span class="o">?</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="p">})</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>
</code></pre></div>



<a name="243837960"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/try%20match%20block/near/243837960" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/try.20match.20block.html#243837960">(Jun 24 2021 at 19:54)</a>:</h4>
<p>Effectively, use <code>?</code> as an early-return in the try block. (Or, in the future, you could use whatever <code>yeet</code>/<code>fail</code>/<code>throw</code> syntax we settle on instead of <code>Err(y)?</code>.)</p>



<a name="243839554"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/try%20match%20block/near/243839554" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/try.20match.20block.html#243839554">(Jun 24 2021 at 20:09)</a>:</h4>
<p>By the way, I've been using "try blocks" on stable for a while, spelled as <code>(|| -&gt; Result&lt;_, _&gt; { Ok({ ... }) })()</code>. Inside such blocks you can use <code>return Err(e)</code> to return an error to the block, or <code>x?</code> to try-unwrap an error targeting the block</p>



<a name="243839704"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/try%20match%20block/near/243839704" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/try.20match.20block.html#243839704">(Jun 24 2021 at 20:10)</a>:</h4>
<p><code>try { ... }</code> will of course be a welcome simplification, but you can basically do the same thing without it</p>



<a name="243841018"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/try%20match%20block/near/243841018" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/try.20match.20block.html#243841018">(Jun 24 2021 at 20:22)</a>:</h4>
<p><span class="user-mention" data-user-id="271719">@Mario Carneiro</span> I'm currently doing the same. And in async code, I'm using <code>let result: Result&lt;_, _&gt; = async { ... }.await</code> for the same purpose.</p>



<a name="243954630"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/try%20match%20block/near/243954630" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/try.20match.20block.html#243954630">(Jun 25 2021 at 18:42)</a>:</h4>
<p>I've definitely also done the </p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="o">..</span><span class="p">.)</span><span class="w"> </span>-&gt; <span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nb">Some</span><span class="p">(</span><span class="k">match</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="w">        </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nb">None</span><span class="w"></span>
<span class="w">    </span><span class="p">})</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>routine before.  I still can't decide how I feel about it.</p>



<a name="243958230"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/try%20match%20block/near/243958230" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/try.20match.20block.html#243958230">(Jun 25 2021 at 19:14)</a>:</h4>
<p><span class="user-mention silent" data-user-id="239881">Josh Triplett</span> <a href="#narrow/stream/122651-general/topic/try.20match.20block/near/243837847">said</a>:</p>
<blockquote>
<p>What about something like this?</p>
<p><div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">temp</span>: <span class="nb">Result</span><span class="o">&lt;</span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kr">try</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nb">Ok</span><span class="p">(</span><span class="k">match</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">Pattern</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">value</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">Pattern</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">value</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="nb">Err</span><span class="p">(</span><span class="n">y</span><span class="p">)</span><span class="o">?</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="p">})</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>
</code></pre></div><br>
</p>
</blockquote>
<p>the problem with having <code>Err(e)?</code> be the "solution" is that its not known to the compiler (specifically typechecker I think?) to diverge, so you end up needing to do stuff like <code>{ Err(e)?; unreachable!(); }</code> which feels pretty gross.</p>



<a name="243969657"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/try%20match%20block/near/243969657" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/try.20match.20block.html#243969657">(Jun 25 2021 at 20:54)</a>:</h4>
<p>You could make a macro for that</p>



<a name="243984374"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/try%20match%20block/near/243984374" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/try.20match.20block.html#243984374">(Jun 26 2021 at 00:28)</a>:</h4>
<p><span class="user-mention" data-user-id="209168">@Thom Chiovoloni</span> That'll be an advantage of the <code>yeet</code> construct: it'll be known to diverge.</p>



<a name="243997588"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/try%20match%20block/near/243997588" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/try.20match.20block.html#243997588">(Jun 26 2021 at 06:13)</a>:</h4>
<p><code>match Err::&lt;!, _&gt;(y)? {}</code> works without the <code>unreachable!</code></p>



<a name="243997627"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/try%20match%20block/near/243997627" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/try.20match.20block.html#243997627">(Jun 26 2021 at 06:14)</a>:</h4>
<p>it's not really something you want to write though</p>



<a name="244000140"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/try%20match%20block/near/244000140" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/try.20match.20block.html#244000140">(Jun 26 2021 at 07:23)</a>:</h4>
<p><code>Err(y)?</code> could work if inference started treating it as <code>Err::&lt;!, _&gt;(y)</code> instead of <code>Err::&lt;(), _&gt;(y)</code>.  I don't know if that's one of the changes in the never type stabilization plan, though.</p>



<a name="244046453"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/try%20match%20block/near/244046453" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/try.20match.20block.html#244046453">(Jun 27 2021 at 02:54)</a>:</h4>
<p><code>match x { } else yeet Err</code></p>



<a name="244046574"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/try%20match%20block/near/244046574" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/try.20match.20block.html#244046574">(Jun 27 2021 at 02:58)</a>:</h4>
<p><span class="user-mention silent" data-user-id="281757">Jubilee</span> <a href="#narrow/stream/122651-general/topic/try.20match.20block/near/244046453">said</a>:</p>
<blockquote>
<p><code>match x { } else yeet Err</code></p>
</blockquote>
<p>i like this even though its no different than just having a <code>_ =&gt; yeet Err</code> branch</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>